package com.terrace.web.tagext.easyui;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.terrace.web.constant.EasyuiConstant;

/**
 * 表单
 * 
 * @author jiangyg
 *
 */
public class FormTag extends TagSupport {

	private static final long serialVersionUID = 2964782883457389276L;

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(FormTag.class);

	/**
	 * 表单name
	 */
	private String name;

	/**
	 * 表单提交路径
	 */
	private String action;
	
	/**
	 * 表单布局
	 */
	private String layout = EasyuiConstant.message("css.form.layout");
	
	/**
	 * 提交前处理函数
	 */
	private String onSubmit;

	/**
	 * 是否刷新表格
	 */
	private String refreshGrid;

	@Override
	public int doStartTag() throws JspException {
		try {
			JspWriter out = this.pageContext.getOut();
			StringBuffer sb = new StringBuffer();
			sb.append("<link rel=\"stylesheet\" type=\"text/css\" href=\"plug-in/static/css/form.layout.css\">");
			sb.append("<form id=\"" + name + "\" name=\"" + name + "\" action=\"" + action + "\" class=\"" + layout + "\" method=\"post\">");
			out.print(sb.toString());
		} catch (Exception e) {
			logger.error("生成表单标签出错：", e);
		}
		return EVAL_PAGE;
	}

	@Override
	public int doEndTag() throws JspException {
		try {
			JspWriter out = this.pageContext.getOut();
			StringBuffer sb = new StringBuffer();
			sb.append("<button type=\"button\" name=\"hideSubmitBtn\" style=\"display: none;\"></button>");
			sb.append("</form>");
			sb.append("<script type=\"text/javascript\">");
			sb.append("$(function() {");
			
			// 表单处理
			sb.append("$('form[name=\"" + name + "\"]').submit(function() {");
			
			// ajax请求发送前执行验证和函数
			sb.append("var customSubmitRev = true;");
			if (StringUtils.isNotBlank(onSubmit)) {
				sb.append("var res=" + onSubmit + "();if(res==true || res==false)customSubmitRev=res;");
			}
			sb.append("if(!customSubmitRev || !$(this).form('enableValidation').form('validate')){return false;}");
			
			// ajax表单请求
			sb.append("$.ajax({");
			sb.append("type:'POST',");
			sb.append("dataType:'json',");
			sb.append("url:'" + action + "',");
			sb.append("beforeSend:function() {");
			sb.append("$.messager.progress({text : '" + EasyuiConstant.message("easyui.submit.loadMsg") + "',interval : 100});");
			sb.append("},");
			sb.append("data:$(this).serialize(),");
			sb.append("success:function(data){");
			sb.append("$.messager.progress('close');");
			sb.append("var api = frameElement.api; opener = api.opener;");
			sb.append("if (data.status=='10000' || data.status=='10001' || data.status=='10002' ");
			sb.append("|| data.status=='10003' || data.status=='10004' || data.status=='10009'){");
			sb.append("opener.msgTip(data.message);");
			sb.append("api.close();");
			if (StringUtils.isNotBlank(refreshGrid)) {
				sb.append("opener." + refreshGrid + "ReloadGridTable();");
			}
			sb.append("}else{");
			sb.append("opener.msgTip(data.message);");
			sb.append("}");
			sb.append("}");
			sb.append("});");
			
			sb.append("return false;");
			sb.append("});");
			
			/**
			sb.append("$('form[name=\"" + name + "\"]').submit(function() {");
			sb.append("$(this).form('submit', {");
			sb.append("url:'" + action + "',");
			sb.append("onSubmit:function(param){");
			sb.append("var customSubmitRev = true;");
			if (StringUtils.isNotBlank(onSubmit)) {
				sb.append("var res=" + onSubmit + "(param);if(res==true || res==false)customSubmitRev=res;");
			}
			sb.append("if(customSubmitRev && $(this).form('validate')){");
			sb.append("$.messager.progress({text : '" + EasyuiConstant.message("easyui.submit.loadMsg") + "',interval : 100});");
			sb.append("return true;}else{return false;}");
			sb.append("},");
			sb.append("success:function(data){");
			sb.append("data = jQuery.parseJSON(data);");
			sb.append("$.messager.progress('close');");
			sb.append("var api = frameElement.api; opener = api.opener;");
			sb.append("if (data.status=='10000' || data.status=='10001' || data.status=='10002' ");
			sb.append("|| data.status=='10003' || data.status=='10004' || data.status=='10009'){");
			sb.append("opener.msgTip(data.message);");
			sb.append("api.close();");
			if (StringUtils.isNotBlank(refreshGrid)) {
				sb.append("opener." + refreshGrid + "ReloadGridTable();");
			}
			sb.append("}else{");
			sb.append("opener.msgTip(data.message);");
			sb.append("}");
			sb.append("}");
			sb.append("});");
			sb.append("return false;");
			sb.append("});");
			**/
			
			// 提交按钮事件
			sb.append("$('button[name=\"hideSubmitBtn\"]').click(function() {");
			sb.append("$('form[name=\"" + name + "\"]').submit();");
			sb.append("});");
			
			sb.append("});");
			sb.append("</script>");
			out.print(sb.toString());
		} catch (Exception e) {
			logger.error("生成表单标签出错：", e);
		}
		return EVAL_PAGE;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getLayout() {
		return layout;
	}

	public void setLayout(String layout) {
		this.layout = layout;
	}

	public String getOnSubmit() {
		return onSubmit;
	}

	public void setOnSubmit(String onSubmit) {
		this.onSubmit = onSubmit;
	}

	public String getRefreshGrid() {
		return refreshGrid;
	}

	public void setRefreshGrid(String refreshGrid) {
		this.refreshGrid = refreshGrid;
	}

}
